/*
 * Copyright 2007-2008  Lukasz Laszko lukaszlaszko@gmail.com
 * 
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software. 
 */
package org.pg.biomedics.asr.data;

import java.util.*;

import org.pg.biomedics.asr.model.*;

/**
 * Data Access Object for task results
 * 
 * @author Lukasz Laszko
 */
public class ResultManager extends BaseDataManager 
{
    // <editor-fold desc="Constructors">
    
    /**
     * Creates a new instance of ResultManager. Data access framework is initailized 
     * according to rules defined in given configuration file
     * @param sqlMapConfig iBatis SQL mapper configuration file
     * @throws org.pg.biomedics.asr.data.DataAccessException
     */
    public ResultManager(String sqlMapConfig) throws DataAccessException
    {
        super(sqlMapConfig);
    }
    
    /**
     * Creates new instance of ResultManager. Provides ORM mapper initialization
     * @throws org.pg.biomedics.asr.data.DataAccessException
     */
    public ResultManager() throws DataAccessException
    {
        super();
    }
    
    // </editor-fold>
    
    // <editor-fold desc="Methods">
    
    /**
     * Inserts a new result into the database
     * @param result new result entity
     * @return inserted result
     * @throws org.pg.biomedics.asr.data.DataAccessException
     */
    public Result insertResult(Result result) throws DataAccessException
    {
         try
        {
            getSqlMapClient().insert("Results.InsertResult", result);
            throwInsertResultException(result);
            
            return result;
        }
        catch (Exception ex)
        {
            throw new DataAccessException("Can't insert result", ex);
        }
    }
    
    /**
     * Gets list of results associated to the given task
     * @param task task entity
     * @return list of task results
     * @throws org.pg.biomedics.asr.data.DataAccessException
     */
    public Result[] getResults(Task task) throws DataAccessException
    {
        try
        {
            Map parameters = new HashMap();
            parameters.put("taskId", task.getId());
            
            getSqlMapClient().insert("Results.GetResults", parameters);
            
            List results = (List)parameters.get("results");
            return (Result[])results.toArray(new Result[0]);
        }
        catch (Exception ex)
        {
            throw new DataAccessException("Can't set task state", ex);
        }
    }
    
    // </editor-fold>
    
    // <editor-fold desc="Support methods">
    
    private void throwInsertResultException(Result result) 
            throws DataAccessException
    {
        switch (result.getId())
        {
            case -1:
                throw new DataAccessException(String.format("Task %i doesn't exist", result.getTaskId()));
            case -2:
                throw new DataAccessException(String.format("Result type %s doesn't exist", result.getTypeCode()));
        }
    }
    
    // </editor-fold>
}
